package com.koron.system.unityUser.schedule;

import com.koron.common.core.business.configsettings.utils.ConfigInfoTool;
import com.koron.common.core.util.StringUtils;
import com.koron.system.unityUser.service.UnityUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * description: 统一用户同步定时任务
 * @author huangsj
 * @date 2023/7/3 11:02
 */
@Slf4j
@Component
@EnableScheduling
public class UnityOrgUserTask {

    @Value("{userNameMd5:null}")
    private String userNameMd5;

    @Value("{passwordMd5:null}")
    private String passwordMd5;

    @Autowired
    private UnityUserService unityUserService;

    @Scheduled(cron = "0 0 2 * * ?")
    public void callInUpAllOrgNode() {
        String isNeedCallTask = ConfigInfoTool.getConfigByCode("system", "org").get("isNeedCallTask");
        String environment = ConfigInfoTool.getConfigByCode("system", "org").get("environment");
        if ("1".equals(isNeedCallTask)&&"zsj".equals(environment)) {
            //登录获取权限码
            String zsj_username = ConfigInfoTool.getConfigByCode("system", "unifyPlatApi").get("zsj_username");
            String zsj_pwd = ConfigInfoTool.getConfigByCode("system", "unifyPlatApi").get("zsj_pwd");
            String auth="";
            if (StringUtils.isNotEmpty(zsj_username)&&StringUtils.isNotEmpty(zsj_pwd)){
                log.info("统一用户授权账号密码正常！");
                auth= unityUserService.hello(null,zsj_username,zsj_pwd).getData();
            }
            try {
                //同步组织
                 unityUserService.organizationList(auth,null);
                log.info("同步统一组织成功");
            } catch (Exception e) {
                log.error("同步统一组织失败:{},{}", e.getMessage(),e);
            }

            try {
                //call staff task
                unityUserService.UserList(auth,null,null,null);
                log.info("同步统一用户成功");
            } catch (Exception e) {
                log.error("同步统一用户失败:{},{}", e.getMessage(),e);
            }
        }
    }
}
